<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 15-4-30
 * Time: 下午5:19
 */

class member extends adminBase{
    function __construct() {
        parent::__construct();
        $this->cms->setPageTitle("会员管理");
        $this->cms->setControlFile("admin/tpl/member/member_control.json");
        $admin_info=$this->user->getLoginInfo();
        $user=UserMode::init();
        $levels=$user->getLevels($admin_info['id']);
        if(!$user->checkLevels(5,$levels)){
            $r['message']="您不具有此权限！";
            View::displayAsHtml($r,"plugin/state/tips.php");
            exit();
        }
    }

    function indexTask($page=1,$enable=null){
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $member=UserModule::init('user');
        list($r['member'],$member_total)=$member->getList($countOfPage,$offset,$enable);
        $r['pages']=new Page($page,$member_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $r['userType']=array("shop"=>"商家用户","user"=>"普通用户","service"=>"服务站用户","both"=>"服务站/商家用户");
        $r['enable']=$enable;
        
        //获取地址
        $commonmode=CommonMode::init();
        $r['province_list']=$commonmode->get_regions(1,1);
        $this->cms->tableScene($r,"admin/tpl/member/member.php");
    }
    

    function memberDetailTask($id){
        $user=UserMode::init();
        $service=ServiceMode::init();
        $r['user_info']=$user->getDetail($id);
        $r['service']=$service->getDetail($r['user_info']['service_id']);
        $this->cms->formScene($r,"admin/tpl/member/memberDetail.php");
    }

    function tradeDetailTask($user_id,$page){
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $user_id=isset($user_id)?$user_id:-1;
        $service=ServiceMode::init();
        $shop=ShopMode::init();
        $money=MoneyMode::init();
        list($r['money_info'],$money_total)=$money->getList($user_id,null,$countOfPage,$offset);
        if(!$money_total){
            $r['message']="该用户暂无交易！";
            View::displayAsHtml($r,"plugin/state/tips.php");
        }else{
            $userInUser=UserModule::init('user');
            $r['money_info']=is_array($r['money_info'])?$r['money_info']:array();
            foreach($r['money_info'] as $v){
                $serviceId=$service->getServiceId($v['user_id']);
                $service_info=$service->getDetail($serviceId);
                $r[$v['user_id']]['service_name']=isset($service_info)?$service_info['service_name']:null;
                $shopId=$shop->getShopId($v['user_id']);
                $shop_info=$shop->getDetail($shopId);
                $r[$v['user_id']]['shop_name']=isset($shop_info)?$shop_info['company']:null;
                $user_inUser=$userInUser->getDetail($v['user_id']);
                $r[$v['user_id']]['user_id']=$user_inUser['username'];
            }
            $r['event_type']=array("applyMoney"=>"提现申请","serviceFee"=>"服务费","shopSell"=>"商家货款",""=>"用户支付");
            $url=WebRouter::init()->getQuestion("user_id={$user_id}&page=");
            $r['pages']=new Page($page,$money_total,$countOfPage);
            $r['pages']->setPageCallback(function($page)use($url){
                return $url.$page;
            });
            $r['flag']="member";
            $r['user_id']=$user_id;
            $this->cms->tableScene($r,"admin/tpl/money/money_info.php");
        }

    }

    function checkMemberTask($page=1,$enable=null,$phone,$name,$province=null,$city=null,$district=null){
        $province_id=(int)$province;
       $city_id=(int)$city;
       $district_id=(int)$district;
        
        $url=WebRouter::init()->getQuestion("name=$name&phone=$phone&province=$province&city=$city&district=$district&page=");
        $page=$page>=1? $page: 1;
        $countOfPage=20;
        $offset=($page-1)*$countOfPage;
        $result=UserChildModule::init("user")->getQuerydetail($phone,$name,$province_id,$city_id,$district_id,$countOfPage,$offset);
        $res=  is_array($result)?$result:'';
        if($res){
        list($r['member'],$member_total)=$res;
        $r['pages']=new Page($page,$member_total,$countOfPage);
        $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
        $r['userType']=array("shop"=>"商家用户","user"=>"普通用户","service"=>"服务站用户","both"=>"服务站/商家用户");
        $r['enable']=$enable;
        
        //获取地址
        $commonmode=CommonMode::init();
        $r['province_list']=$commonmode->get_regions(1,1);
        $this->cms->tableScene($r,"admin/tpl/member/member.php");
       
        }else{
                $r['message']="暂无该用户信息！";
                View::displayAsHtml($r,"plugin/state/tips.php");
        }
    }

    function checkPassTask($page,$id,$enable=null){
        $user=UserMode::init();
        $url=WebRouter::init()->getQuestion("page=");
        $page=$page>=1? $page: 1;
        $offset=((int)$page==1)? '0':($page-1)*20;
        if($user->auth($id)){
            $member=UserModule::init('user');
            list($r['member'],$member_total)=$member->getList(20,$offset,$enable);
            $r['pages']=new Page($page,$member_total);
            $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
            $r['userType']=array("shop"=>"商家用户","user"=>"普通用户","service"=>"服务站用户","both"=>"服务站/商家用户");
            $r['enable']=$enable;
            $this->cms->tableScene($r,"admin/tpl/member/member.php");
        }
    }

    function checkCancelTask($id,$enable=null){
        $user=UserMode::init();
        $url=WebRouter::init()->getQuestion("page=");
        $page=isset($_GET['page'])&&($_GET['page']>0)? (int)$_GET['page']:1;
        $offset=((int)$page==1)? '0':($page-1)*20;
        if($user->auth($id,0)){
            $member=UserModule::init('user');
            list($r['member'],$member_total)=$member->getList(20,$offset,$enable);
            $r['pages']=new Page($page,$member_total);
            $r['pages']->setPageCallback(create_function('$page','return "'.$url.'".$page;'));
            $r['userType']=array("shop"=>"商家用户","user"=>"普通用户","service"=>"服务站用户","both"=>"服务站/商家用户");
            $r['enable']=$enable;
            $this->cms->tableScene($r,"admin/tpl/member/member.php");
        }
    }

    function ajaxurlTask($type,$parent){
       $parent=(int)$parent;
       $type=(int)$type;
        $commonmode=CommonMode::init();
        $list=$commonmode->get_regions($type,$parent);
        $str="";
        $str.=" <option value='0'>请选择</option>";
        foreach($list as $k=>$v){
            $str.="<option value='".$v['region_id']."'> ".$v['region_name']."</option>";
        }
        die(json_encode($str));
    }
} 